package com.weixing.mall.provider.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weixing.mall.provider.api.service.ProductStockServiceRpc;
import com.weixing.mall.provider.mapper.*;
import com.weixing.mall.provider.model.domain.Goods;
import com.weixing.mall.provider.model.domain.GoodsImg;
import com.weixing.mall.provider.model.domain.GoodsIntro;
import com.weixing.mall.provider.model.domain.GoodsSpec;
import com.weixing.mall.provider.model.query.GoodsQuery;
import com.weixing.mall.provider.model.vo.GoodsVo;
import com.weixing.mall.provider.service.RecycleBinService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author wx
 * @Date 2019/8/8 16:28
 * @Version 1.0
 */
@Service
public class RecycleBinServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements RecycleBinService {

    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsImgMapper goodsImgMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private GoodsSpecMapper goodsSpecMapper;
    @Autowired
    private GoodsIntroMapper goodsIntroMapper;
    @Autowired
    private GoodsPartitionMapper goodsPartitionMapper;
    @Autowired
    private GoodsSceneMapper goodsSceneMapper;
    //彻底删除
    @Override
    public void deleteCompletely(int[] ids) {
        goodsMapper.delAll(ids);
        for (int id : ids) {
            goodsImgMapper.deleteByGoodsId((long) id);
            productMapper.deleteByGoodsId((long) id);
            goodsSpecMapper.deleteByGoodsId((long) id);
            goodsIntroMapper.deleByGoodsId((long) id);
            goodsPartitionMapper.deleteByGoodsId((long) id);
            goodsSceneMapper.deleteByGoodsId((long) id);
        }
    }

    //恢复

    @Override
    public void Recovery(int[] ids) {
        //查询删除前状态
        List<GoodsVo> list= goodsMapper.selectStatusBeforeDelete(ids);
        goodsMapper.Recovery(list);
    }
    @Override
    public IPage<GoodsVo> selectList(Page page, GoodsQuery goodsQuery) {

        IPage<GoodsVo> pageList = goodsMapper.selectRecycleList(page,goodsQuery);
        List<GoodsVo> list = pageList.getRecords();

        for (GoodsVo goodsVo : list) {
            List<GoodsImg> goodsImgs = goodsImgMapper.selectList(goodsVo.getId());
            if(goodsImgs.size()>0){
                goodsVo.setGoodsImg(goodsImgs.get(0).getGoodsImg());
            }
        }
        pageList.setRecords(list);
        return pageList;

    }

    @Override
    public Integer findOrder(int[] ids) {
        return goodsMapper.findOrder(ids);
    }

    @Override
    public Integer findProduct(int[] ids) {
        return goodsMapper.findProduct(ids);
    }
}
